#!/bin/bash

# 设置模块目录（根据你的项目结构调整）
MODULES_DIR="."

# 查找所有包含 build.gradle 或 build.gradle.kts 的目录
for MODULE in $(find "$MODULES_DIR" -type f -name "build.gradle" -o -name "build.gradle.kts" | xargs dirname); do
    RELATIVE_PATH="${MODULE#$MODULES_DIR/}"
    GRADLE_PROJECT_PATH=":${RELATIVE_PATH//\//}"

    echo "🔍 Found module: $GRADLE_PROJECT_PATH"

    # 执行发布命令
    echo "🚀 Publishing $GRADLE_PROJECT_PATH ..."
    ./gradlew "$GRADLE_PROJECT_PATH:publishAllPublicationsToCentralPortal" || true

    if [ $? -eq 0 ]; then
        echo "✅ Successfully published: $GRADLE_PROJECT_PATH"
    else
        echo "❌ Failed to publish: $GRADLE_PROJECT_PATH"
        exit 1
    fi
done